$(function () {
    let $editPriority = $(".btn-edit");
    $editPriority.click(function (){
        console.log(this);
        let id = $(this).parents("tr").data("id");
        let priority = $(this).parents("tr").data("priority");
        let title = $(this).parents("tr").data("title");
        fAlert.alertOneInput({
            title: "编辑热门新闻优先级",
            text: "你当前正在编辑热门文章" + title + "的优先级",
            placeholder: "请输入优先级",
            confirmCallback: function(inputValue){
                console.log(typeof inputValue);
                console.log(typeof priority);
                if(inputValue == priority){
                    fAlert.alertError("优先级未变化");
                }
                if(!(/^[1-5]$/).test(inputValue)){
                    fAlert.alertError("优先级为1~5");
                }
                let data = {
                    "priority": inputValue
                };
                $.ajax({
                    url: "/x-admin/hot-news/" + id + "/",
                    type: "POST",
                    data: JSON.stringify(data),
                    contentType: "application/json;charset=utf-8",
                    dataType: "JSON",
                })
                    .done(function (res) {
                        if(res.errno === "0"){
                            fAlert.alertSuccessToast("修改成功");
                            setTimeout(()=>{
                                window.location.reload();
                            }, 1000)
                        }
                        else{
                            fAlert.alertError(res.errmsg);
                        }
                    })
                    .fail(function (res) {
                        fAlert.alertError("网络出现错误，请刷新重试");
                    })
            }
        })
    });

    let $deletePriority = $(".btn-del");
    $deletePriority.click(function () {
        let hotNewsId = $(this).parents("tr").data("id");
        console.log(typeof hotNewsId);
        $.ajax({
            url: "/x-admin/hot-news/" + hotNewsId + "/",
            type: "delete",
            dataType: "json",
        })
            .done(function (res) {
                if(res.errno === "0"){
                    fAlert.alertSuccessToast("删除成功");
                    setTimeout(()=>{
                        window.location.reload();
                    }, 1000);
                }
                else{
                    fAlert.alertErrorToast(res.errmsg);
                }
            })
            .fail(()=>{
                fAlert.alertErrorToast("网络出现问题，请刷新重试");
            })
    });

    let $addPriority = $("#btn-add-news");
    $addPriority.click(function () {
        window.location.href = "/x-admin/hot-add/";
    });

    let $select = $("#category-select");
    $select.change(function () {
        let id = $select.val();
        console.log(id);
        let data = {
            id: id,
        };
        $.ajax({
            url: "http://www.blogcloud.info/x-admin/hot-add/",
            type: "POST",
            data: JSON.stringify(data),
            contentType: "application/json;charset=utf-8",
            dataType: "json"
        })
            .done(function (res) {
                if(res.errno === "0"){
                    let news_list = res.data.news_list;
                    let $newsSelect = $("#news-select");
                    news_list.forEach(function (news) {
                        let content = `<option value="${news.id}">${news.title}</option>`;
                        $newsSelect.append(content);
                    })
                }
                else{
                    fAlert.alertErrorToast(res.errmsg);
                }
            })
            .fail(function(){
                fAlert.alertErrorToast("网络超时，请刷新重试");
            })
    })

    let $saveHot = $("#save-btn");
    $saveHot.click(function () {
        let tag_id = $("#category-select").val();
        let news_id = $("#news-select").val();
        let priority = $("#priority").val();
        if(news_id==0){
            fAlert.alertErrorToast("请选择要添加的文章");
        }
        if(priority==0){
            fAlert.alertErrorToast("请选择文章优先级");
        }
        data = {
            news_id: news_id,
            priority: priority
        }

        $.ajax({
            url: "http://www.blogcloud.info/x-admin/hot-add/",
            type: "put",
            data: JSON.stringify(data),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
        })
    });
})